package com.coder4j.platform.service;

import com.coder4j.platform.entity.SysContentEntity;
import com.coder4j.platform.entity.SysDictEntity;
import com.coder4j.platform.mapper.SysContentMapper;
import com.coder4j.platform.vo.SysContentQuery;
import com.coder4j.platform.vo.SysContentVO;
import com.coder4j.platform.vo.SysDictQuery;
import com.coder4j.platform.vo.UserMenuVO;
import com.coder4j.starter.tools.util.ListUtils;
import com.coder4j.starter.web.BaseServiceImpl;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 内容表，笔记、文章、教程等
 * @author keller
 * @date 2025-05-31 19:08:27
 */
@Service
@Slf4j
public class SysContentService extends BaseServiceImpl<SysContentMapper,SysContentEntity,SysContentVO> {

    @Resource
    private SysContentMapper mapper;

    /**
     * 字典
     */
    @Resource
    @Lazy
    private SysDictService dictService;

    @Override
    public SysContentVO entity2Vo(SysContentEntity entity) {
        SysContentVO vo = new SysContentVO();
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }

    @Override
    public SysContentEntity vo2Entity(SysContentVO vo) {
        SysContentEntity entity = new SysContentEntity();
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }

    /**
     * 根据类型查询内容的树形结构
     * @return
     */
    public List<UserMenuVO> queryListByType(String type){

        List<UserMenuVO> list = new ArrayList<>();

        SysDictQuery dictQuery = new SysDictQuery();
        dictQuery.setType(type);
        List<SysDictEntity> dictList = dictService.baseQueryList(dictQuery);

        if(ListUtils.isEmpty(dictList)){
            return list;
        }
        List<Long> ids = dictList.stream().map(SysDictEntity::getId).collect(Collectors.toList());

        List<SysContentEntity> contentList = this.mapper.getListByGroupIds(ids);

        if(ListUtils.isEmpty(contentList)){
            return list;
        }

        Map<Long, List<SysContentEntity>> map = contentList.stream()
                        .collect(Collectors.groupingBy(SysContentEntity::getGroupId));
        for (SysDictEntity dict : dictList) {
            UserMenuVO vo = new UserMenuVO();
            vo.setType("group");
            vo.setLabel(dict.getLabel());
            vo.setKey(dict.getId() + "");

            List<SysContentEntity> subList = map.get(dict.getId());
            if(ListUtils.isEmpty(subList)){
                continue;
            }

            List<UserMenuVO> children = new ArrayList<>();
            for (SysContentEntity content : subList) {
                UserMenuVO child = new UserMenuVO();
                child.setKey(content.getId() + "");
                child.setLabel(content.getTitle());

                children.add(child);
            }

            vo.setChildren(children);
            list.add(vo);
        }

        return list;
    }
}